package com.ww.springboot.boot.test;

public class TaxNumberValidator {

    /**
     * 校验法国税号（SIREN 或 SIRET）的合法性
     * @param taxNumber 税号字符串
     * @return 是否合法
     */
    public static boolean isValidTaxNumber(String taxNumber) {
        if (taxNumber == null || !taxNumber.matches("\\d{9}|\\d{14}")) {
            return false;
        }

        int[] weights = {2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1};
        int sum = 0;

        for (int i = 0; i < taxNumber.length(); i++) {
            int digit = Character.getNumericValue(taxNumber.charAt(i));
            int weightedDigit = digit * weights[i];
            sum += weightedDigit > 9 ? weightedDigit - 9 : weightedDigit;
        }

        return sum % 10 == 0;
    }

    public static void main(String[] args) {
        String siren = "05654358413";
        //String siret = "322 271 627 00016";

        System.out.println("SIREN: " + isValidTaxNumber(siren.replaceAll(" ", ""))); // 输出: true
        //System.out.println("SIRET: " + isValidTaxNumber(siret.replaceAll(" ", ""))); // 输出: true
    }
}
